// ui  layer
var g_uiLayer;
var UILayer = cc.Node.extend({
    _tanks : 0,
    _money : 0,
    /////////////////////
    _menuBar : null,
    _tanksLabel : null,
    _moneyLabel : null,
    /////////////////////
    _pause : null,
    _resume : null,
    /////////////////////
    ctor:function () {
        this._super();
        g_uiLayer=this;
        this.initMessage();
        return true;
    },
    onEnter : function(){
        this._super();
        this.addMenuBar();
        this.addMessage();
        this.addPauseMenu();
    },
    onExit : function(){
        this._super();
    },
    //////////////////////////////////////////////////////
    initMessage : function(){
        this._tanks = GetTankAmount();
    },
    //////////////////////////////////////////////////////
    addMenuBar : function(){
        var filename = MainResource("menuBar.png");
        this._menuBar = new cc.Sprite( filename );
        this._menuBar.setPosition( cc.p( cc.winSize.width*0.5,cc.winSize.height-39 ) );
        this.addChild(this._menuBar);
    },
    addMessage : function(){
        var filename = MainResource("num0.png"),yOff = 20;
        this._moneyLabel = cc.LabelAtlas.create(this._money, filename,26, 35, '0');
        this._moneyLabel.setPosition( cc.p( 190, yOff ) );
        this._menuBar.addChild(this._moneyLabel);

        filename = MainResource("num1.png");
        this._tanksLabel = cc.LabelAtlas.create(this._tanks, filename,26, 35, '0');
        this._tanksLabel.setPosition( cc.p( 520, yOff ) );
        this._menuBar.addChild(this._tanksLabel);
    },
    addPauseMenu:function(){
        var filename = MainResource("mResume.png");
        this._resume = new cc.MenuItemImage(filename,filename,this.onResume , this);
        this._resume.setPosition( cc.p( cc.winSize.width - 160, cc.winSize.height-40 ) );
        this._resume.setVisible(false);
        filename = MainResource("mPause.png");
        this._pause = new cc.MenuItemImage(filename,filename,this.onPause , this);
        this._pause.setPosition( cc.p( cc.winSize.width - 160, cc.winSize.height-40 ) );
        var menu = cc.Menu.create( this._resume,this._pause );
        menu.setPosition(cc.p(0,0));
        this.addChild( menu );
    },
    //////////////////////////////////////////////////////
    addMoney : function(add){
        this._money+=add;
        this._moneyLabel.setString(this._money);
    },
    getMoney : function(){
        return  this._money;
    },
    getTanks : function(){
        return  this._tanks;
    },
    ///////////////////////////////////////////
    onPause : function(sender){
        cc.log("pause game");
        this.pauseGame();
    },
    onResume : function(sender){
        cc.log("resume game");
        this.resumeGame();
    },
    /////////////////////////////////////
    pauseGame : function(){
        this._resume.setVisible(true);
        this._pause.setVisible(false);
        cc.director.pause();
    },
    resumeGame : function(){
        this._resume.setVisible(false);
        this._pause.setVisible(true);
        cc.director.resume();
    }
});
AddUiLayer=function(parent){
    if(parent){
        var layer = new UILayer();
        parent.addChild( layer,MW.ORDER.UI );
        cc.log("add ui layer down");
    }else{
        cc.log("invalid parent");
    }
}
RemoveUiLayer=function(clear){
    if(g_uiLayer){
        g_uiLayer.removeFromParent(clear);
        cc.log("remove ui layer down, clear: "+clear);
    }else{
        cc.log("g_uiLayer is invalid");
    }
}
///////////////////////////////////////////
GetUILayer=function(){
    if(g_uiLayer) {
        cc.log("get  g_uiLayer");
        return g_uiLayer;
    }else{
        cc.log("g_uiLayer invalid");
        return null;
    }
}
///////////////////////////////////////////
GetUIMoney=function(){
    var ui=GetUILayer();
    return ui.getMoney();
}
AddUIMoney=function(add){
    var ui=GetUILayer();
    ui.addMoney(add);
}
